package zozopo.app.operations;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import zozopo.database.BusiDatabaseManager;
import zozopo.database.SqlMachine;
import zozopo.operations.comm.OperatingService;

public class ApplicationOS extends OperatingService{
	
	//private static final Log logger = LogFactory.getLog(DepartmentOS.class);
	
	public ApplicationOS(Connection conn){
		super(conn);
	}
	
	public ApplicationOS(){
		this.databaseConnection = new BusiDatabaseManager();
		this.conn = databaseConnection.getConnection();
	}
	
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public boolean insert(ApplicationTB applicationBackground){
		return false;
	}
	
	/**
	 * @version 1.0
	 * @see 
	 */
	public List<ApplicationTB> selectListBySql(String sql){
		List<ApplicationTB> list = null;
		ResultSet resultSet = databaseConnection.executeQuery(conn, sql);
		
		if(resultSet != null){
				try {
					list = new ArrayList<ApplicationTB>();
					while(resultSet.next()){
						ApplicationTB bgApp = new ApplicationTB();
						
						bgApp.setId_application(resultSet.getInt(ApplicationTB.ID_APPLICATION));
						
						bgApp.setMember_applistcation(resultSet.getInt(ApplicationTB.MEMBER_APPLICATION));
						bgApp.setUsedtimes_application(resultSet.getInt(ApplicationTB.USEDTIMES_APPLICATION));
						
						bgApp.setCode_application(resultSet.getString(ApplicationTB.CODE_APPLICATION));
						
						bgApp.setUrl_application(resultSet.getString(ApplicationTB.URL_APPLICATION));
						bgApp.setKeyurl_application(resultSet.getString(ApplicationTB.KEY_APPLICATION));
						
						bgApp.setName_application(resultSet.getString(ApplicationTB.NAME_APPLICATION));
						bgApp.setIcon60_application(resultSet.getString(ApplicationTB.ICON60_APPLICATION));
						bgApp.setIcon100_application(resultSet.getString(ApplicationTB.ICON100_APPLICATION));
						bgApp.setDes_application(resultSet.getString(ApplicationTB.DES_APPLICATION));
						bgApp.setCreateddate_application(resultSet.getString(ApplicationTB.CREATEDDATE_APPLICATION));
						
						bgApp.setIsnormal_application(resultSet.getBoolean(ApplicationTB.ISNORMAL_APPLICATION));
						
						list.add(bgApp);
					}
					resultSet.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
		}
		
		return list;
	
	}
	
	public List<ApplicationTB> select(){
		
		List<ApplicationTB> list = null;
		String sql = "SELECT * FROM " + ApplicationTB.TABLE_NAME ; 
		list = selectListBySql(sql);
		
		return list;
	}
	
	public List<ApplicationTB> select(String sort){
		
		List<ApplicationTB> list = null;
		String sql = "SELECT * FROM " + ApplicationTB.TABLE_NAME 
		+ " ORDER BY " +ApplicationTB.ID_APPLICATION+" "+ sort; 
		list = selectListBySql(sql);
		
		return list;
	}
	
	
	public ApplicationTB get(int id){
		
		String sql = "SELECT * FROM " + ApplicationTB.TABLE_NAME + " where " + ApplicationTB.ID_APPLICATION + "=" + id;
		ApplicationTB obj = null;
		List<ApplicationTB> objList = selectListBySql(sql);
		obj = objList.get(0);
		return obj;
	}  
	
	/**
	 * @version 1.0
	 * @author Shea
	 */
	public int getMaxId(){
		//String sql = "select * from "+ReleaseTB.TABLE_NAME+" order by  "+ReleaseTB.RELEASE_DATE+" desc limit "+beginPageNum+","+pageSize; 
		SqlMachine sqlMachine = new SqlMachine(conn);
		int maxId = sqlMachine.getMaxId(ApplicationTB.ID_APPLICATION, ApplicationTB.TABLE_NAME);
		return maxId;
	}

	

}
